/**
 * 学员模块操作功能
 */

//--------------------------------------------------------------------------
//
//	学员模块操作
//
//--------------------------------------------------------------------------
function setNewStudent( d )
{
	
	var myDate = new Date();
	
	var year = myDate.getFullYear();
	var month = myDate.getMonth()+1;
	var mydate  = myDate.getDate();
	var mytime = year+"-"+month+"-"+mydate;
	
	
	// 结构添加
	var i = $( "#studentlst" ).find("table").length;
	var h = "<table>";
	h += "<tr class='fields'>";
	h += "<th colspan='2'><span rel='title_nickname'>学员名</span><a rel='btnDelete' class='right'>[ 删除 ]</a></th>";
	h += "</tr>";
	h += "<tr>";
	h += "<th width='25%'>姓名</th>";
	h += "<td width='75%'><input rel='st_type' type='hidden'/><input rel='st_key' type='hidden'/><input rel='st_name'/> <select rel='st_sex'>";
	h += "<option value=''>性别</option>";
	h += "<option>男</option>";
	h += "<option>女</option>";
	h += "</select> <input rel='st_age' maxlength='2' size='3' placeholder='年龄' />岁</td>";
	h += "</tr>";
	h += "<tr>";
	h += "<th width='25%'>生日日期</th>";
	h += "<td width='75%'><input type='text' readyonly='true' id='st_birthday' value="+mytime+" rel='DATE::Y-m-d' size='15' maxlength='15' /> <a rel='btnGetAge'>[ 计算年龄 ]</a>";
	h += "<p class='caption'>点击弹出日期选择框顶部可切换月、日、年选择界面</p></td>";
	h += "</tr>";
	h += "<tr>";
	h += "<th width='25%'>所在学校</th>";
	h += "<td width='75%'><input rel='st_school' placeholder='学校名称' size='50' /></td>";
	h += "</tr>";
	h += "<tr>";
	h += "<th width='25%'>训练类型</th>";
	h += "<td width='75%' rel='typelist'>";

	h += "<label style='margin-right:20px;'><input type='checkbox' rel='type_zx' value='a'/>中心训练</label>";
	h += "<label style='margin-right:20px;'><input type='checkbox' rel='type_jt' value='b'/>家庭训练</label>";
	h += "<label style='margin-right:20px;'><input type='checkbox' rel='type_qn' value='c'/>全脑训练</label>";
	h += "<p class='caption'>若为“中心、家庭”结合则同时勾选“中心训练、家庭训练”，“全脑训练”只能与“中心训练”同时选择</p>";
	
	h += "</td>";
	h += "</tr>";

	h += "<tr>";
	h += "<th width='25%'>中心训练信息</th>";
	h += "<td width='75%'>";
	h += "<label>课时数量：</label><input type='text' rel='st_lesson' placeholder='中心训练课时数量' size='15' />";
	h += "<label style='margin-left:20px;'>学员卡号密码：</label><input type='text' rel='st_card' placeholder='学员卡号和密码' size='20' />";
	h += "<p class='caption'>请一定正确填写学员卡号和密码，以“卡号/密码”的格式填写</p>";
	h += "</td>";
	h += "</tr>";

	h += "<tr>";
	h += "<th width='25%'>家庭训练信息</th>";
	h += "<td width='75%'>";
	h += "<label>仪器编号：</label><input type='text' rel='st_serial' placeholder='仪器编号、密码' size='15' />";
	h += "<label style='margin-left:20px;'>训练截止时间：</label><input type='text' rel='st_deadline' size='15' maxlength='15' />&nbsp元";
	h += "</td>";
	h += "</tr>";

	h += "<tr>";
	h += "<th width='25%' style='padding:15px 10px 15px 10px;'>学员特征和其它资料";
	h += "<p class='caption'>咨询或测评时表现的特征和情况</p>";
	h += "</th>";
	h += "<td width='75%'><div rel='st_feature' contenteditable='true'>" +
			"<div style='color:#C679B5;'contenteditable='false'>测评前填写：</div>" +
			"<div>● 测评前学员情况：</div>" +
			"<div>● 预约编号：</div>" +
			"<div>● 是否报价：</div>" +
			"<div style=color:#C679B5;'contenteditable='false'>测评情况：</div>" +
			"<div>● 问题持续时间：</div>" +
			"<div>● 曾经检查测试：</div>" +
			"<div>● 学员问题类型：</div>" +
			"<div>● 学员情况表现：</div>" +
			"<div style='color:#C679B5;'contenteditable='false'>CNS+脑电情况：</div>"+
			"<div>&nbsp;&nbsp;&nbsp;CNS：</div>" +
			"<div>&nbsp;&nbsp;&nbsp;脑电：</div>" +
			"<div>● 家长信息：</div>" +
			"● 重点推荐课程：" +
			"<div>&nbsp;&nbsp;&nbsp;1.训练侧重点：</div>" +
			"<div>&nbsp;&nbsp;&nbsp;2.家长顾虑点：</div>" +
			"<div>&nbsp;&nbsp;&nbsp;3.家长期望值：</div>" +
			"<div>&nbsp;&nbsp;&nbsp;4.其他需注意的问题：</div></div></div></td>";
	h += "</tr>";
	h += "</table>";

	$( "#studentlst" ).append( h );

	var tbl = $( "#studentlst" ).find( "table" );
	tbl.find( "#st_birthday" ).Zebra_DatePicker(
			{ format: 'Y-m-d', show_icon: false} );
	tbl.find( "[rel='st_name']" ).keyup( name_changed );
	tbl.find( "[rel='title_nickname']" ).parent().click( name_row_click );
	tbl.find( "[rel='title_nickname']" ).parent().css( { "cursor": "pointer" } );
	tbl.find( "[rel='btnDelete']" ).click( st_delete_handler );
	
	tbl.find( "[rel='type_zx']" ).change( { tbl: tbl }, type_changed );
	tbl.find( "[rel='type_jt']" ).change( { tbl: tbl }, type_changed );
	tbl.find( "[rel='type_qn']" ).change( { tbl: tbl }, type_changed );

	tbl.find( "[rel='st_deadline']" ).Zebra_DatePicker( { format: 'Y-m-d', show_icon: false } );
	// 分析年龄
	tbl.find( "[rel='btnGetAge']" ).click(
			{ btar: tbl.find( "#st_birthday" ),tar: tbl.find( "input[rel='st_age']" ) }, function( e )
			{
				parse_age_bydate( e.data.btar, e.data.tar, function( tar, age )
				{
					tar.val( age );
				} );
			} );

	
	
	// 赋值数据
	if( !empty( d ) )
	{
		var key = d.key;
		var name = defaultVal( d.nickname );
		var sex = defaultVal( d.sex );
		var age = defaultVal( d.age );
		var birthday = defaultVal( d.birthday );
		var school = defaultVal( d.school );
		var feature = defaultVal( d.feature );
		var type = defaultVal( d.type );
		var serial = defaultVal( d.serial );
		var lesson = defaultVal( d.lesson );
		var card = defaultVal( d.card );
		var deadline = defaultVal( d.deadline );

		tbl.find( "[rel='st_key']" ).val( key );
		tbl.find( "[rel='title_nickname']" ).html( name );
		tbl.find( "[rel='st_name']" ).val( name );
		tbl.find( "[rel='st_sex']" ).val( sex );
		tbl.find( "[rel='st_age']" ).val( age );
		tbl.find( "#st_birthday" ).val( birthday );
		tbl.find( "[rel='st_school']" ).val( school );
		tbl.find( "[rel='st_money']" ).val( money );
		tbl.find( "[rel='st_feature']" ).html( feature );
		
		$("[rel='st_trade']:checked[value='"+trade+"']").prop("checked",true);
		// 训练类型
		if( type.length > 0 )
		{
			var type_arr = type.split( "" );
			var size = type_arr.length;

			for( var i = 0; i < size; i++ )
			{
				$( "[rel='typelist'] :checkbox[value='" + type_arr[ i ] + "']" ).prop( "checked", true );
			}
		}
		tbl.find( "[rel='st_serial']" ).val( serial );
		tbl.find( "[rel='st_deadline']" ).val( deadline );
		tbl.find( "[rel='st_lesson']" ).val( lesson );
		tbl.find( "[rel='st_card']" ).val( card );
		update_by_type( tbl );

		// 设置随机 ID 用于格式化内容
		tbl.find( "[rel='st_feature']" ).attr( "id", hash( 12 ) );
	}
}
 	
	

function type_changed( e )
{
	var tbl = e.data.tbl;
	var qn = tbl.find( "[rel='type_qn']" );
	var zx = tbl.find( "[rel='type_zx']" );
	
	update_by_type( tbl );

	if( qn.prop( "checked" ) && !zx.prop( "checked" ) )
	{
		qn.prop( "checked", false );
		infopad.err( "全脑训练只适用于中心训练类型，请先勾选中心训练！" );
	}
}

function update_by_type( tbl )
{
	var serial = tbl.find( "[rel='st_serial']" );
	var lesson = tbl.find( "[rel='st_lesson']" );
	var zx = tbl.find( "[rel='type_zx']" );
	var jt = tbl.find( "[rel='type_jt']" );
	if( zx.is( ":checked" ) )
	{
		lesson.parent().parent().show();
	}
	else
	{
		lesson.parent().parent().hide();
	}

	if( jt.is( ":checked" ) )
	{
		serial.parent().parent().show();
	}
	else
	{
		serial.parent().parent().hide();
	}
}

function parse_age_bydate( btar, tar, callback )
{
	// 计算年龄
	var age = 0;
	var birthday = btar.val();
	if( empty( birthday ) )
	{
		infopad.err( "生日日期未设置，无法计算年龄！" );
		return;
	}

	var bd_date = new Date( birthday );
	var by = bd_date.getFullYear();
	var bm = bd_date.getMonth() + 1;
	var bd = bd_date.getDate();

	var now = new Date();
	var ny = now.getFullYear();
	var nm = now.getMonth() + 1;
	var nd = now.getDate();

	// 默认年数差就是年龄
	age = ny - by;

	// 若月或者日未到，则要减去一岁
	if( ( bm > nm || ( bm == nm && bd > nd ) ) && age > 0 )
		age--;

	// 设置回调
	if( typeof callback == "function" )
		callback( tar, age );
}

function birthday_changed( date, b, h, n )
{
	var tbl = n.parent().parent().parent();

	parse_age_bydate( tbl.find( "#st_birthday" ), tbl.find( "input[rel='st_age']" ), function( tar,
			age )
	{
		tar.val( age );
	} );
}

/**
 * 编辑学员名称时，在表头也同样显示更新
 */
function name_changed()
{
	var tbl = $( this ).parent().parent().parent();
	tbl.find( "[rel='title_nickname']" ).html( $( this ).val() );
}

/**
 * 点击学员资料的表头时，隐藏表体
 */
function name_row_click()
{
	var tbl = $( this ).parent().parent();

	if( tbl.find( "[rel='st_type']" ).val() == "deleted" )
		return;

	var visible = tbl.find( "tr:not(:first)" ).visible();
	var trs = tbl.find( "tr:not(:first)" );

	if( visible )
	{
		trs.hide();
	}
	else
	{
		trs.show();
	}
}

function st_delete_handler( e )
{
	e.stopPropagation();
	var row = $( this ).parent();
	var tbl = $( this ).parent().parent().parent();

	if( $( this ).text() == "[ 删除 ]" )
	{
		$( this ).text( "[ 恢复 ]" );
		row.css( { "background": "#ccc url(img/cancel.png) no-repeat 20px 50%", color: "#fff" } );
		tbl.find( "tr:not(:first)" ).hide();
		tbl.find( "[rel='st_type']" ).val( "deleted" );
	}
	else
	{
		$( this ).text( "[ 删除 ]" );
		row.css( { "background": "", color: "" } );
		tbl.find( "tr:not(:first)" ).show();
		tbl.find( "[rel='st_type']" ).val( "" );
	}
}


function defaultVal( val )
{
	if( empty( val ) )
		return "";
	return val;
}

//--------------------------------------------------------------------------
//
//	表单数据收集
//
//--------------------------------------------------------------------------

/**
 * 分析并返回学员列表数据（可能多个学员）
 */
function get_students()
{
	var student = [];
	$( "#studentlst > table" ).each( function( id )
	{
		var tbl = $( this );
		var sd = {};

		sd.type = empty( tbl.find( "[rel='st_type']" ).val() ) ? "update" : "del";
		sd.key = tbl.find( "[rel='st_key']" ).val();

		// 若是新增加的学员资料，且需要删除，不必提交
		if( sd.type == "del" && empty( sd.key ) )
		{
			tbl.remove();
			return true;
		}

		// 分析是添加新记录类型还是更新数据类型
		if( sd.type == "update" ){
			
			var id = tbl.find("[rel='st_id']").val();
			
			if( empty( sd.key ) )
				sd.type = "add";
			if(id!=null && id.length>0){
				sd.id = id;
			}else{
				sd.id = "";
			}
			
			// 只有更新或者添加时才增加详细数据提交
			sd.nickname = tbl.find( "[rel='st_name']" ).val();
			sd.sex = tbl.find( "[rel='st_sex']" ).val();
			sd.age = tbl.find( "[rel='st_age']" ).val();
			sd.birthday = tbl.find( "#st_birthday" ).val();
			sd.school = tbl.find( "[rel='st_school']" ).val();
			sd.money = tbl.find("[rel='st_money']").val();
			sd.feature = tbl.find( "[rel='st_feature']" ).html() ;
			
			//是否成交
			var trades = "";
			
			tbl.find( "[rel='yes_no'] :checked" ).each(function (idx){
				trades = $( this ).val();
			});
			sd.trade = trades;
			// 训练类型
			var types = "";
			tbl.find( "[rel='typelist'] :checked" ).each( function( idx )
			{
				types += $( this ).val();
			} );
			sd.types = types;

			// 其它
			sd.lesson = tbl.find( "[rel='st_lesson']" ).val().replace(/[^0-9]/ig,"");
			sd.card = tbl.find( "[rel='st_card']" ).val();
			sd.serial = tbl.find( "[rel='st_serial']" ).val();
			sd.deadline = tbl.find( "[rel='st_deadline']" ).val();
	
		}
		student.push( sd );
	} );

	return student;
}
